using MediatR;
using Microsoft.AspNetCore.Mvc;
using Warehouse.API.APP.Commands;
using Warehouse.Dto;
using Warehouse.Services.IServices;

namespace Warehouse.API.Controllers
{
    /// <summary>
    /// 调拨单控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class AllocationController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IAllocationService _allocationService;

        public AllocationController(IMediator mediator, IAllocationService allocationService)
        {
            _mediator = mediator;
            _allocationService = allocationService;
        }

        /// <summary>
        /// 创建调拨单
        /// </summary>
        /// <param name="request">创建调拨单请求</param>
        /// <returns>创建结果</returns>
        [HttpPost("CreateAllocation")]
        public async Task<IActionResult> CreateAllocation([FromBody] CreateAllocationRequest request)
        {
            try
            {
                var command = new CreateAllocationCommand
                {
                    TransferType = request.TransferType,
                    ApplyDate = request.ApplyDate,
                    FromWarehouseId = request.FromWarehouseId,
                    ToWarehouseId = request.ToWarehouseId,
                    HandlerName = request.HandlerName,
                    ApplicantName = request.ApplicantName,
                    Remark = request.Remark,
                    SkipValidation = request.SkipValidation,
                    Lines = request.Lines
                };

                var result = await _mediator.Send(command);

                return Ok(new
                {
                    code = 200,
                    message = "创建成功",
                    data = result
                });
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new
                {
                    code = 400,
                    message = ex.Message
                });
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(new
                {
                    code = 400,
                    message = ex.Message
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    code = 500,
                    message = "服务器内部错误",
                    error = ex.Message
                });
            }
        }

        /// <summary>
        /// 获取调拨单列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>调拨单列表</returns>
        [HttpGet("GetTransferList")]
        public async Task<IActionResult> GetTransferList(int pageIndex = 1, int pageSize = 20)
        {
            try
            {
                var result = await _allocationService.GetTransferListAsync(pageIndex, pageSize);

                return Ok(new
                {
                    code = 200,
                    message = "查询成功",
                    data = result
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    code = 500,
                    message = "服务器内部错误",
                    error = ex.Message
                });
            }
        }

        /// <summary>
        /// 获取调拨单详情
        /// </summary>
        /// <param name="transferId">调拨单ID</param>
        /// <returns>调拨单详情</returns>
        [HttpGet("GetTransferDetail/{transferId}")]
        public async Task<IActionResult> GetTransferDetail(int transferId)
        {
            try
            {
                var result = await _allocationService.GetTransferDetailAsync(transferId);

                if (result == null)
                {
                    return NotFound(new
                    {
                        code = 404,
                        message = "调拨单不存在"
                    });
                }

                return Ok(new
                {
                    code = 200,
                    message = "查询成功",
                    data = result
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    code = 500,
                    message = "服务器内部错误",
                    error = ex.Message
                });
            }
        }

        /// <summary>
        /// 获取调拨单明细
        /// </summary>
        /// <param name="transferId">调拨单ID</param>
        /// <returns>调拨单明细</returns>
        [HttpGet("GetTransferLines/{transferId}")]
        public async Task<IActionResult> GetTransferLines(int transferId)
        {
            try
            {
                var result = await _allocationService.GetTransferLinesAsync(transferId);

                return Ok(new
                {
                    code = 200,
                    message = "查询成功",
                    data = result
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    code = 500,
                    message = "服务器内部错误",
                    error = ex.Message
                });
            }
        }

        /// <summary>
        /// 调拨出库
        /// </summary>
        /// <param name="request">调拨出库请求</param>
        /// <returns>出库结果</returns>
        [HttpPost("TransferOut")]
        public async Task<IActionResult> TransferOut([FromBody] TransferOutRequest request)
        {
            try
            {
                var result = await _allocationService.TransferOutAsync(request);

                return Ok(new
                {
                    code = 200,
                    message = "调拨出库成功",
                    data = result
                });
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new
                {
                    code = 400,
                    message = ex.Message
                });
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(new
                {
                    code = 400,
                    message = ex.Message
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    code = 500,
                    message = "服务器内部错误",
                    error = ex.Message
                });
            }
        }

        /// <summary>
        /// 调拨入库
        /// </summary>
        /// <param name="request">调拨入库请求</param>
        /// <returns>入库结果</returns>
        [HttpPost("TransferIn")]
        public async Task<IActionResult> TransferIn([FromBody] TransferInRequest request)
        {
            try
            {
                var result = await _allocationService.TransferInAsync(request);

                return Ok(new
                {
                    code = 200,
                    message = "调拨入库成功",
                    data = result
                });
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new
                {
                    code = 400,
                    message = ex.Message
                });
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(new
                {
                    code = 400,
                    message = ex.Message
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    code = 500,
                    message = "服务器内部错误",
                    error = ex.Message
                });
            }
        }


    }
}

